<?php
$htmlent2utf8 = array(
 '&nbsp;' => "\xC2\xA0", '&iexcl;' => "\xC2\xA1", '&cent;' => "\xC2\xA2", 
 '&pound;' => "\xC2\xA3", '&curren;' => "\xC2\xA4", '&yen;' => "\xC2\xA5", 
 '&brvbar;' => "\xC2\xA6", '&sect;' => "\xC2\xA7", '&uml;' => "\xC2\xA8", 
 '&copy;' => "\xC2\xA9", '&ordf;' => "\xC2\xAA", '&laquo;' => "\xC2\xAB", 
 '&not;' => "\xC2\xAC", '&shy;' => "\xC2\xAD", '&reg;' => "\xC2\xAE", 
 '&macr;' => "\xC2\xAF", '&deg;' => "\xC2\xB0", '&plusmn;' => "\xC2\xB1", 
 '&sup2;' => "\xC2\xB2", '&sup3;' => "\xC2\xB3", '&acute;' => "\xC2\xB4", 
 '&micro;' => "\xC2\xB5", '&para;' => "\xC2\xB6", '&middot;' => "\xC2\xB7", 
 '&cedil;' => "\xC2\xB8", '&sup1;' => "\xC2\xB9", '&ordm;' => "\xC2\xBA", 
 '&raquo;' => "\xC2\xBB", '&frac14;' => "\xC2\xBC", '&frac12;' => "\xC2\xBD", 
 '&frac34;' => "\xC2\xBE", '&iquest;' => "\xC2\xBF", '&Agrave;' => "\xC3\x80", 
 '&Aacute;' => "\xC3\x81", '&Acirc;' => "\xC3\x82", '&Atilde;' => "\xC3\x83", 
 '&Auml;' => "\xC3\x84", '&Aring;' => "\xC3\x85", '&AElig;' => "\xC3\x86", 
 '&Ccedil;' => "\xC3\x87", '&Egrave;' => "\xC3\x88", '&Eacute;' => "\xC3\x89", 
 '&Ecirc;' => "\xC3\x8A", '&Euml;' => "\xC3\x8B", '&Igrave;' => "\xC3\x8C", 
 '&Iacute;' => "\xC3\x8D", '&Icirc;' => "\xC3\x8E", '&Iuml;' => "\xC3\x8F", 
 '&ETH;' => "\xC3\x90", '&Ntilde;' => "\xC3\x91", '&Ograve;' => "\xC3\x92", 
 '&Oacute;' => "\xC3\x93", '&Ocirc;' => "\xC3\x94", '&Otilde;' => "\xC3\x95", 
 '&Ouml;' => "\xC3\x96", '&times;' => "\xC3\x97", '&Oslash;' => "\xC3\x98", 
 '&Ugrave;' => "\xC3\x99", '&Uacute;' => "\xC3\x9A", '&Ucirc;' => "\xC3\x9B", 
 '&Uuml;' => "\xC3\x9C", '&Yacute;' => "\xC3\x9D", '&THORN;' => "\xC3\x9E", 
 '&szlig;' => "\xC3\x9F", '&agrave;' => "\xC3\xA0", '&aacute;' => "\xC3\xA1", 
 '&acirc;' => "\xC3\xA2", '&atilde;' => "\xC3\xA3", '&auml;' => "\xC3\xA4", 
 '&aring;' => "\xC3\xA5", '&aelig;' => "\xC3\xA6", '&ccedil;' => "\xC3\xA7", 
 '&egrave;' => "\xC3\xA8", '&eacute;' => "\xC3\xA9", '&ecirc;' => "\xC3\xAA", 
 '&euml;' => "\xC3\xAB", '&igrave;' => "\xC3\xAC", '&iacute;' => "\xC3\xAD", 
 '&icirc;' => "\xC3\xAE", '&iuml;' => "\xC3\xAF", '&eth;' => "\xC3\xB0", 
 '&ntilde;' => "\xC3\xB1", '&ograve;' => "\xC3\xB2", '&oacute;' => "\xC3\xB3", 
 '&ocirc;' => "\xC3\xB4", '&otilde;' => "\xC3\xB5", '&ouml;' => "\xC3\xB6", 
 '&divide;' => "\xC3\xB7", '&oslash;' => "\xC3\xB8", '&ugrave;' => "\xC3\xB9", 
 '&uacute;' => "\xC3\xBA", '&ucirc;' => "\xC3\xBB", '&uuml;' => "\xC3\xBC", 
 '&yacute;' => "\xC3\xBD", '&thorn;' => "\xC3\xBE", '&yuml;' => "\xC3\xBF", 
 '&OElig;' => "\xC5\x92", '&oelig;' => "\xC5\x93", 
 '&Scaron;' => "\xC5\xA0", '&scaron;' => "\xC5\xA1", '&Yuml;' => "\xC5\xB8", 
 '&circ;' => "\xCB\x86", '&tilde;' => "\xCB\x9C", '&ensp;' => "\xE2\80\82", 
 '&emsp;' => "\xE2\80\83", '&thinsp;' => "\xE2\80\89", '&zwnj;' => "\xE2\80\8C", 
 '&zwj;' => "\xE2\80\8D", '&lrm;' => "\xE2\80\8E", '&rlm;' => "\xE2\80\8F", 
 '&ndash;' => "\xE2\80\93", '&mdash;' => "\xE2\80\94", '&lsquo;' => "\xE2\80\98", 
 '&rsquo;' => "\xE2\80\99", '&sbquo;' => "\xE2\80\9A", '&ldquo;' => "\xE2\80\9C", 
 '&rdquo;' => "\xE2\80\9D", '&bdquo;' => "\xE2\80\9E", '&dagger;' => "\xE2\80\A0", 
 '&Dagger;' => "\xE2\80\A1", '&permil;' => "\xE2\80\B0", '&lsaquo;' => "\xE2\80\B9", 
 '&rsaquo;' => "\xE2\80\BA", '&euro;' => "\xE2\x82\xAC", '&fnof;' => "\xC6\x92", 
 '&Alpha;' => "\xCE\x91", '&Beta;' => "\xCE\x92", '&Gamma;' => "\xCE\x93", 
 '&Delta;' => "\xCE\x94", '&Epsilon;' => "\xCE\x95", '&Zeta;' => "\xCE\x96", 
 '&Eta;' => "\xCE\x97", '&Theta;' => "\xCE\x98", '&Iota;' => "\xCE\x99", 
 '&Kappa;' => "\xCE\x9A", '&Lambda;' => "\xCE\x9B", '&Mu;' => "\xCE\x9C", 
 '&Nu;' => "\xCE\x9D", '&Xi;' => "\xCE\x9E", '&Omicron;' => "\xCE\x9F", 
 '&Pi;' => "\xCE\xA0", '&Rho;' => "\xCE\xA1", '&Sigma;' => "\xCE\xA3", 
 '&Tau;' => "\xCE\xA4", '&Upsilon;' => "\xCE\xA5", '&Phi;' => "\xCE\xA6", 
 '&Chi;' => "\xCE\xA7", '&Psi;' => "\xCE\xA8", '&Omega;' => "\xCE\xA9", 
 '&alpha;' => "\xCE\xB1", '&beta;' => "\xCE\xB2", '&gamma;' => "\xCE\xB3", 
 '&delta;' => "\xCE\xB4", '&epsilon;' => "\xCE\xB5", '&zeta;' => "\xCE\xB6", 
 '&eta;' => "\xCE\xB7", '&theta;' => "\xCE\xB8", '&iota;' => "\xCE\xB9", 
 '&kappa;' => "\xCE\xBA", '&lambda;' => "\xCE\xBB", '&mu;' => "\xCE\xBC", 
 '&nu;' => "\xCE\xBD", '&xi;' => "\xCE\xBE", '&omicron;' => "\xCE\xBF", 
 '&pi;' => "\xCF\x80", '&rho;' => "\xCF\x81", '&sigmaf;' => "\xCF\x82", 
 '&sigma;' => "\xCF\x83", '&tau;' => "\xCF\x84", '&upsilon;' => "\xCF\x85", 
 '&phi;' => "\xCF\x86", '&chi;' => "\xCF\x87", '&psi;' => "\xCF\x88", 
 '&omega;' => "\xCF\x89", '&thetasym;' => "\xCF\x91", '&upsih;' => "\xCF\x92", 
 '&piv;' => "\xCF\x96", '&bull;' => "\xE2\80\A2", '&hellip;' => "\xE2\80\A6", 
 '&prime;' => "\xE2\80\B2", '&Prime;' => "\xE2\80\B3", '&oline;' => "\xE2\80\BE", 
 '&frasl;' => "\xE2\81\84", '&weierp;' => "\xE2\84\98", '&image;' => "\xE2\84\91", 
 '&real;' => "\xE2\84\9C", '&trade;' => "\xE2\84\A2", '&alefsym;' => "\xE2\84\B5", 
 '&larr;' => "\xE2\86\90", '&uarr;' => "\xE2\86\91", '&rarr;' => "\xE2\86\92", 
 '&darr;' => "\xE2\86\93", '&harr;' => "\xE2\86\94", '&crarr;' => "\xE2\86\B5", 
 '&lArr;' => "\xE2\87\90", '&uArr;' => "\xE2\87\91", '&rArr;' => "\xE2\87\92", 
 '&dArr;' => "\xE2\87\93", '&hArr;' => "\xE2\87\94", '&forall;' => "\xE2\88\80", 
 '&part;' => "\xE2\88\82", '&exist;' => "\xE2\88\83", '&empty;' => "\xE2\88\85", 
 '&nabla;' => "\xE2\88\87", '&isin;' => "\xE2\88\88", '&notin;' => "\xE2\88\89", 
 '&ni;' => "\xE2\88\8B", '&prod;' => "\xE2\88\8F", '&sum;' => "\xE2\88\91", 
 '&minus;' => "\xE2\88\92", '&lowast;' => "\xE2\88\97", '&radic;' => "\xE2\88\9A", 
 '&prop;' => "\xE2\88\9D", '&infin;' => "\xE2\88\9E", '&ang;' => "\xE2\88\A0", 
 '&and;' => "\xE2\88\A7", '&or;' => "\xE2\88\A8", '&cap;' => "\xE2\88\A9", 
 '&cup;' => "\xE2\88\AA", '&int;' => "\xE2\88\AB", '&there4;' => "\xE2\88\B4", 
 '&sim;' => "\xE2\88\BC", '&cong;' => "\xE2\89\85", '&asymp;' => "\xE2\89\88", 
 '&ne;' => "\xE2\89\A0", '&equiv;' => "\xE2\89\A1", '&le;' => "\xE2\89\A4", 
 '&ge;' => "\xE2\89\A5", '&sub;' => "\xE2\8A\82", '&sup;' => "\xE2\8A\83", 
 '&nsub;' => "\xE2\8A\84", '&sube;' => "\xE2\8A\86", '&supe;' => "\xE2\8A\87", 
 '&oplus;' => "\xE2\8A\95", '&otimes;' => "\xE2\8A\97", '&perp;' => "\xE2\8A\A5", 
 '&sdot;' => "\xE2\8B\85", '&lceil;' => "\xE2\8C\88", '&rceil;' => "\xE2\8C\89", 
 '&lfloor;' => "\xE2\8C\8A", '&rfloor;' => "\xE2\8C\8B", '&lang;' => "\xE2\8C\A9", 
 '&rang;' => "\xE2\8C\AA", '&loz;' => "\xE2\97\8A", '&spades;' => "\xE2\99\A0", 
 '&clubs;' => "\xE2\99\A3", '&hearts;' => "\xE2\99\A5", '&diams;' => "\xE2\99\A6" ); 

/*
  convert HTML entities (as defined in XHTML 1.0 spec) to UTF-8-encoded Unicode
  EXCEPT: lt, gt, amp, quot, and apos
*/
function htmlent_to_utf8($entstring) {
   global $htmlent2utf8;
   return strtr($entstring, $htmlent2utf8);
}

/* 
   convert ISO Latin 9 string (ISO-8859-15) to UTF-8 string
*/
function latin9_to_utf8($latin9) {
   $latin_len = strlen($latin9);
   for($i=0; $i < $latin_len; $i++) {
      $zeichen = ord($latin9[$i]);
      if($zeichen <= 0x7F)
         $utf8 .= chr($zeichen);
      elseif($zeichen == 0xA4)  
         $utf8 .= "\xE2\x82\xAC";    // euro sign
      else {
         switch($zeichen) {
            case 0xBC: $zeichen = 0x152; break;  // OE ligature
            case 0xBD: $zeichen = 0x153; break;  // oe ligature
            case 0xA6: $zeichen = 0x160; break;  // S with caron
            case 0xA8: $zeichen = 0x161; break;  // s with caron 
            case 0xBE: $zeichen = 0x178; break;  // Y with diaeresis
            case 0xB4: $zeichen = 0x17D; break;  // Z with caron
            case 0xB8: $zeichen = 0x17E; break;  // z with caron
         }
         $utf8 .= chr(0xC0 | ($zeichen >> 6)) . chr(0x80 | ($zeichen & 0x3F));
      }
   }
   return $utf8;
}

/* 
   convert ISO Latin 1 string (ISO-8859-1) to UTF-8 string
*/
function latin1_to_utf8($latin1) {
   $latin_len = strlen($latin1);
   for($i=0; $i < $latin_len; $i++) {
      $zeichen = ord($latin1[$i]);
      if($zeichen <= 0x7F)
         $utf8 .= chr($zeichen);
      else {
         $utf8 .= chr(0xC0 | ($zeichen >> 6)) . chr(0x80 | ($zeichen & 0x3F));
      }
   }
   return $utf8;
}
?>